package com.zyy.smartcity1129.parking;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.hardware.lights.Light;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.JsonObject;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.ParkRecordInfo;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.util.RvAd;

import java.util.List;

import okhttp3.RequestBody;

/**
 * @ClassName: ParkingRecordFragment$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 4/12/2021$
 * @Version: 1.0
 */
public class ParkingRecordFragment extends BaseFragment {
    private ImageButton imgParkRecord1;
    private TextView tvParkRecord1;
    private ImageButton imgParkRecord2;
    private TextView tvParkRecord2;
    private RecyclerView rvParkRecord;
    private Button btnParkRecord;
    String date, date2;
    String dateF, dateS;
    String time1, time2;
    RvAd<ParkRecordInfo.RowsBean> rvAd;
    private Button btnParkRecord2;

    @Override
    protected int layId() {
        return R.layout.f_park_record;
    }

    @Override
    protected String title() {
        return "停车记录";
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return false;
    }

    @Override
    protected void init(View iv) {
        initView(iv);
        rvParkRecord.setLayoutManager(new LinearLayoutManager(getContext()));
        th(() -> {
            List<ParkRecordInfo.RowsBean> list = Http.getInstance().get("/prod-api/api/park/lot/record/list", null, ParkRecordInfo.class, 1).getRows();
            List<ParkRecordInfo.RowsBean> list1 = list.subList(0, 5);
            ui(() -> {
                initAdapter(list1);
                btnParkRecord.setOnClickListener(view -> {
                    btnParkRecord.setVisibility(View.GONE);
                    initAdapter(list);
                });
            });
        });
        btnParkRecord2.setOnClickListener(view -> {
//            if (!date.equals("") || !date2.equals("")) {
            if (date != null && date2 != null) {
                btnParkRecord.setVisibility(View.VISIBLE);
                th(() ->{
                    List<ParkRecordInfo.RowsBean> list = Http.getInstance().get("/prod-api/api/park/lot/record/list"+"?entryTime="+date+"&outTime="+date2,
                            null, ParkRecordInfo.class, 1).getRows();
                    List<ParkRecordInfo.RowsBean> list1 = list.subList(0, 5);
                    ui(() -> {
                        initAdapter(list1);
                        btnParkRecord.setOnClickListener(view1 -> {
                            btnParkRecord.setVisibility(View.GONE);
                            initAdapter(list);
                        });
                    });
                });
            } else {
                Toast.makeText(getContext(), "请选择日期和时间", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initAdapter(List<ParkRecordInfo.RowsBean> list) {
        rvAd = new RvAd<ParkRecordInfo.RowsBean>(list, R.layout.rv_base) {
            private void initView(View vi) {
                ig = (ImageView) vi.findViewById(R.id.ig);
                lineRvPatient = (LinearLayout) vi.findViewById(R.id.lineRvPatient);
                tv1 = (TextView) vi.findViewById(R.id.tv1);
                tv2 = (TextView) vi.findViewById(R.id.tv2);
                tv3 = (TextView) vi.findViewById(R.id.tv3);
                tv4 = (TextView) vi.findViewById(R.id.tv4);
                tv5 = (TextView) vi.findViewById(R.id.tv5);
                tv6 = (TextView) vi.findViewById(R.id.tv6);
                tv7 = (TextView) vi.findViewById(R.id.tv7);
                imgRvPatient = (ImageView) vi.findViewById(R.id.imgRvPatient);
                ig.setVisibility(View.GONE);
            }

            private ImageView imgRvPatient;
            private TextView tv7;
            private TextView tv6;
            private TextView tv5;
            private TextView tv4;
            private TextView tv3;
            private TextView tv2;
            private TextView tv1;
            private LinearLayout lineRvPatient;
            private ImageView ig;

            @Override
            protected void initR(View vi, ParkRecordInfo.RowsBean rowsBean, int p) {
                initView(vi);
                tv1.setText(rowsBean.getParkName());
                tv2.setText("入场时间: " + rowsBean.getEntryTime());
                tv5.setText("车牌号: " + rowsBean.getPlateNumber());
                tv4.setText("收费金额: " + rowsBean.getMonetary() + "元");
                tv3.setText("出场时间：" + rowsBean.getOutTime());
            }
        };
        rvParkRecord.setAdapter(rvAd);
    }

    private void initView(View iv) {
        imgParkRecord1 = (ImageButton) iv.findViewById(R.id.imgParkRecord1);
        tvParkRecord1 = (TextView) iv.findViewById(R.id.tvParkRecord1);
        imgParkRecord2 = (ImageButton) iv.findViewById(R.id.imgParkRecord2);
        tvParkRecord2 = (TextView) iv.findViewById(R.id.tvParkRecord2);
        rvParkRecord = (RecyclerView) iv.findViewById(R.id.rvParkRecord);
        btnParkRecord = (Button) iv.findViewById(R.id.btnParkRecord);
        btnParkRecord2 = (Button) iv.findViewById(R.id.btnParkRecord2);
        imgParkRecord1.setOnClickListener(view -> {
            new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                    date = year + "-";
                    if ((month + 1) < 10) {
                        date += "0" + (month + 1) + "-";
                    } else {
                        date += (month + 1) + "-";
                    }
                    if (dayOfMonth < 10) {
                        date += "0" + dayOfMonth + " ";
                    } else {
                        date += dayOfMonth + " ";
                    }
                    dateF = date.trim();
                    Log.i("TAG", date);
                    Log.i("TAGF", dateF);
                    new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker timePicker, int hour, int minute) {
                            String mTime = "";
                            if (hour < 10) {
                                mTime += "0" + hour + ":";
                            } else {
                                mTime += hour + ":";
                            }
                            if (minute < 10) {
                                mTime += "0" + minute + ":00";
                            } else {
                                mTime += minute + ":00";
                            }
                            date += mTime;
                            time1 = mTime;
                            Log.i("TAG", date);
                            Log.i("TAGM", time1);
                            tvParkRecord1.setText(date);
                        }
                    }, 0, 0, true).show();
                }
            }, 2021, 11, 1).show();
        });
        imgParkRecord2.setOnClickListener(view -> {
            new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                    date2 = year + "-";
                    if ((month + 1) < 10) {
                        date2 += "0" + (month + 1) + "-";
                    } else {
                        date2 += (month + 1) + "-";
                    }
                    if (dayOfMonth < 10) {
                        date2 += "0" + dayOfMonth + " ";
                    } else {
                        date2 += dayOfMonth + " ";
                    }
                    dateS = date2.trim();
                    Log.i("TAG", date2);
                    Log.i("TAGF", dateS);
                    new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker timePicker, int hour, int minute) {
                            String mTime = "";
                            if (hour < 10) {
                                mTime += "0" + hour + ":";
                            } else {
                                mTime += hour + ":";
                            }
                            if (minute < 10) {
                                mTime += "0" + minute + ":00";
                            } else {
                                mTime += minute + ":00";
                            }
                            date2 += mTime;
                            time2 = mTime;
                            Log.i("TAG", date2);
                            Log.i("TAGM", time2);
                            tvParkRecord2.setText(date2);
                        }
                    }, 0, 0, true).show();
                }
            }, 2021, 11, 1).show();
        });
    }
}
